<#include "common.ftl">
/**
 * @main
 * @module 初始化模块+其他模块的调用的公共函数
 */
$(document).ready(function() {
    var gatherData = function() {
        var rows = getGrid().omGrid("getSelections",true);
        if(rows.length == 0) {
            $.omMessageBox.alert({
                content : "请选择需要数据!!"
            });
            return null;
        }
        return rows;
    };
    
    window.gatherData = gatherData;
    
    var getGrid = function() {
        var activeTabId = $("#make_tab").omTabs("getActivated");
        var tabIndex = $("#make_tab").omTabs("getAlter",activeTabId);
        return $("#grid" + (tabIndex+1));
    };
    
    window.getGrid = getGrid;
    
    /**
     * 设置tab标签页
     */
    $("#make_tab").omTabs({
        lazyLoad : true,
        width : "auto",
        onActivate : function(n, event) {
            getGrid().omGrid("reload");
        }
    });
});

/**
 * @submodule 分页查询+查询模块
 */
$(document).ready(function(){
    var colModel = [
<#list columnBeanList as columnBean>
        {
            header : "${columnBean.property}",
            name : "${columnBean.property}",
            width : 100,
            align : "center"
        },           
</#list>            
        {
            header : "操作",
            name : "",
            width : "autoExpand",
            align : "center"
        }
    ];
    
    var dataSource = "../${entityBean.javaName}!showAllList.action";
    
    /**
     * @method 分页查询功能
     */
    var initGrid = function(target,dataSource) {
        $(target).omGrid({
            dataSource : dataSource,
            limit : 15,
            height : 500,
            showIndex : false,
            singleSelect : true,
            singleSelect : false,
            colModel : colModel
        });
    };
    
    initGrid($("#grid1"),dataSource);
    
    initGrid($("#grid2"),dataSource);
    
    /**
     * @method 绑定查询函数
     */
    $(".query").bind("click",function(e) {
        var params = $("#searchForm").serialize();
        getGrid().omGrid("setData",dataSource + "?" + params);
    });
});

/**
 * @submodule 新增模块
 */
$(document).ready(function(){
    /**
     * @property omValidator OM框架校验对象
     */
    var addValidator = $("#addForm").validate({
        rules : {
<#list columnBeanList as columnBean>
            "${beanObject}.${columnBean.property}" : {
<#if columnBean.fieldType!="Date">            
                maxlength : ${columnBean.dataLength}
</#if>
            }<#if tc_has_next>,</#if> 
</#list>
        },
        errorPlacement : function(error, element) {
            $("<br/>").appendTo(element.parent());
            error.appendTo(element.parent());
        }
    });

    /*
     * @property 新增或者修改dialog
     */
    var addDialog = $("#dialog-form-add").omDialog({
        width : defDialog.width,
        height : defDialog.height,
        autoOpen : defDialog.autoOpen,
        modal : defDialog.modal,
        resizable : defDialog.resizable,
        buttons : [
            {
                text : "提交",
                click : function(message) {
                    if(!addValidator.form()){
                        return;
                    }
                    var url = "../${entityBean.javaName}!add.action";
                    var data = $("#addForm").serialize();
                    $.ajax({
                        type : "POST",
                        url : url,
                        data : data,
                        dataType : "json",
                        success : function(ret) {
                            if(ret.success) {
                                getGrid().omGrid("reload");
                                addDialog.omDialog("close");
                                baseTool.showTip("success",ret.msg);
                            } else {
                                baseTool.showAlert("error",ret.msg);
                            }
                        }
                    });
                }
            },
            {
                text : "取消",
                click : function() {
                    addDialog.omDialog("close");
                }
            }
        ]
    });
    
    /**
     * @event 绑定新增按钮事件
     */
    $(".create").bind("click",function(){
        addValidator.resetForm();
        addDialog.omDialog("option","title","新增");
        addDialog.omDialog("open");
    });
});

/**
 * @submodule 查看详细模块
 */
$(document).ready(function(){
    /**
     * @property omDialog元素
     */
    var showDialog = $("#dialog-form-show").omDialog({
        width : defDialog.width,
        height : defDialog.height,
        autoOpen : defDialog.autoOpen,
        modal : defDialog.modal,
        resizable : defDialog.resizable,
        buttons : [
            {
                text : "取消",
                click : function() {
                    showDialog.omDialog("close");
                }
            }
        ]
    });
    
    var show = function() {
        var rows = gatherData();
        var rowData = rows[0];
<#list columnBeanList as columnBean>
<#if !columnBean.isPrimaryKey>
        $("#${columnBean.property}_show").text(rowData.${columnBean.property});
</#if> 
</#list>
        return true;
    }
    
    /**
     * @event 给show按钮添加查询详细事件
     */
    $(".show").bind("click", function(e) {
        show();
        showDialog.omDialog("option", "title", "查看详细");
        showDialog.omDialog("open");// 显示dialog
    });
});

/**
 * @submodule 更新模块
 */
$(document).ready(function(){
    /**
     * @property omValidator OM框架校验对象
     */
    var updateValidator = $("#updateForm").validate({
        rules : {
<#list columnBeanList as columnBean>
            "${beanObject}.${columnBean.property}" : {
<#if columnBean.isPrimaryKey>
                required : true,
</#if>
<#if columnBean.fieldType!="Date">            
                maxlength : ${columnBean.dataLength}
</#if>               
            }<#if tc_has_next>,</#if> 
</#list>
        },
        errorPlacement : function(error, element) {
            $("<br/>").appendTo(element.parent());
            error.appendTo(element.parent());
        }
    });

    /*
     * @property 修改omDialog
     */
    var updateDialog = $("#dialog-form-update").omDialog({
        width : defDialog.width,
        height : defDialog.height,
        autoOpen : defDialog.autoOpen,
        modal : defDialog.modal,
        resizable : defDialog.resizable,
        buttons : [
            {
                text : "提交",
                click : function(message) {
                    
                }
            },
            {
                text : "取消",
                click : function() {
                    updateDialog.omDialog("close");
                }
            }
        ]
    });
    
    var ajaxUpdate = function() {
        if(!updateValidator.form()){
            return;
        }
        var url = "../${entityBean.javaName}!update.action";
        var data = $("#updateForm").serialize();
        $.ajax({
            type : "POST",
            url : url,
            data : data,
            dataType : "json",
            success : function(ret) {
                if(ret.success) {
                    getGrid().omGrid("reload");
                    updateDialog.omDialog("close");
                    baseTool.showTip("success",ret.msg);
                } else {
                    baseTool.showAlert("error",ret.msg);
                }
            }
        });
    }
    
    var update = function() {
        updateDialog.resetForm();
        var rows = gatherData();
        var rowData = rows[0];
<#list columnBeanList as columnBean>
        $("#${columnBean.property}_update").val(rowData.${columnBean.property});
</#list>
    }
    
    /**
     * @event 绑定修改按钮事件
     */
    $(".modify").bind("click",function(){
        update();
        updateDialog.omDialog("option","title","修改");
        updateDialog.omDialog("open");
    });
});

/**
 * @submodule 删除模块
 */
$(document).ready(function(){
    /**
     * @method doDelete,删除操作函数
     */    
    var ajaxDelete = function() {
        var rows = gatherData();
        var rowData = rows[0];
        var params = new Object();
<#list columnBeanList as columnBean>
<#if columnBean.isPrimaryKey>
        params["${beanObject}.${columnBean.property}"] = rowData["${columnBean.property}"];
</#if>
</#list>
        $.ajax({
            type : "POST",
            url : "../${entityBean.javaName}!delete.action",
            data : params,
            dataType : "json",
            success : function(ret) {
                if(ret.success) {
                    getGrid().omGrid("reload");
                    baseTool.showTip("success",ret.msg);
                } else {
                    baseTool.showAlert("error",ret.msg);
                }
            }
        });
    };
    
    /**
     * @event 事件绑定方法
     */
    $(".remove").bind("click",function(){
        baseTool.showConfirm("确定要删除这条数据码?",function(){
            ajaxDelete();
        });
    });
});